package cn.app.web.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.app.service.DevUserService;

@Controller
@RequestMapping("/devUser")
public class DevUserController {
	
	@Autowired
	private DevUserService devUserService;
	
	@RequestMapping("/disLogin")
	public String disLogin(){
		return "devLogin";
	}
	
	@RequestMapping("/login")
	public String login(HttpSession session,Model model,
						@RequestParam(value="username",required=false)String devcode,
						@RequestParam(value="password",required=false)String devpassword){
		
		SecurityUtils.getSubject().login(new UsernamePasswordToken(devcode, devpassword));
		
		
		//跳转主页.
		return "redirect:/devApp/disMain";
//		DevUser devUser = devUserService.login(devcode,devpassword);
		/*if(devUser != null){
			//登录
			session.setAttribute("devUser", devUser);
			
		}else{
			//登录失败
			model.addAttribute("loginInfo", "用户名或密码错误....");
			return "devLogin";
		}*/
	}
	
	
	
	@RequestMapping("/download")    
    public ResponseEntity<byte[]> download() throws IOException {    
        String path="E:\\pic\\90f70520-3fe2-4999-ab52-1e118d88aeda.jpg";  
        File file=new File(path);  
        HttpHeaders headers = new HttpHeaders();    
        String fileName=new String("90f70520-3fe2-4999-ab52-1e118d88aeda.jpg".getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题  
        headers.setContentDispositionFormData("attachment", fileName);   
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);   
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),    
                                          headers, HttpStatus.CREATED);    
    }  
	
	
	
	
	
	/*//退出登录
	@RequestMapping("/logout")
	public String logout(HttpSession session){
		//讲session域中的用户信息清除
		session.removeAttribute("devUser");
		//跳转到登录页面
		return "devLogin";
	}*/
}
